﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// Common 的摘要说明
/// </summary>
public class CommonScore
{
    public CommonScore()
    {

    }

    /// <summary>
    /// 年级统考存储映射
    /// </summary>
    private static Hashtable gradescore = null;
    public static bool Inited
    {
        get
        {
            if (gradescore != null)
            {
                return true;
            }
            return false;
        }
    }
    /// <summary>
    /// 初始化年级存储表映射
    /// </summary>
    public static void InitGradeScore()
    {
        gradescore = new Hashtable();
        IList<GradeDatastoreEducation> gdes = Container<Meqs.Logic.Interface.IGradeDatastoreEducationLogic>.Instance.FindAll();
        foreach (GradeDatastoreEducation g in gdes)
        {
            gradescore.Add(g.Grade, g.TableName);
        }
    }
    /// <summary>
    /// 获得表名
    /// </summary>
    /// <param name="grade"></param>
    /// <returns></returns>
    public static string GetGradeStoreTable(int grade)
    {
        if (gradescore ==null || grade > gradescore.Count)
        {
            return "";
        }
        return gradescore[grade].ToString();
    }




    /// <summary>
    /// 转换年级
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertGradeNum(int num)
    {
        if (num <= 0 || num > 12) return "";
        string[] GradeStrArr = { "一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "初一", "初二", "初三", "高一", "高二", "高三" };

        return GradeStrArr[num - 1];
    }

    /// <summary>
    /// 转换学期
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertTerm(int term)
    {
        if (term < 1 || term > 2) return "";
        string[] TermStrArr = { "上学期", "下学期" };

        return TermStrArr[term - 1];
    }
    /// <summary>
    /// 统考状态转换
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertTestStatus(int status)
    {
        if (status < 0 || status > 3) return "";
        string[] TermStrArr = { "新建", "已下发", "已提交", "已结束" };

        return TermStrArr[status];
    }

    /// <summary>
    /// 学校用统考状态
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertTestStatusForSchool(int status)
    {
        if (status < 0 || status > 3) return "";
        string[] TermStrArr = { "", "未上报", "已上报" };

        return TermStrArr[status];
    }

    /// <summary>
    /// 文理科转换
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertClassType(int t)
    {
        if (t < 0 || t > 2) return "";
        //不分科就不显示
        string[] ClassType = { "不分科", "文科", "理科" };

        return ClassType[t];
    }

    /// <summary>
    /// 班级等级
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertClassLevel(int l)
    {
        if (l < 0 || l > 1) return "";
        //不分科就不显示
        string[] ClassType = { "普通", "重点" };

        return ClassType[l];
    }

    /// <summary>
    /// 学期
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertSchoolYear(int schoolyear)
    {
        string str = string.Format("{0} - {1} 学年 ", schoolyear, schoolyear + 1);
        return str;
    }

    /// <summary>
    /// 转换学年和学期
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string ConvertYearAndTerm(int schoolyear, int term)
    {
        string str = string.Format("{0} - {1} 学年 {2}", schoolyear, schoolyear + 1, ConvertTerm(term));
        return str;
    }


    /// <summary>
    /// 局级考试列表的特殊字符转换
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static void ConvertEduExamHtmlCode(ref List<VExamsEducation> list)
    {
        if (list.Count > 0)
        {
            foreach (VExamsEducation item in list)
            {
                TextWriter stringWriter = new StringWriter();

                System.Web.HttpContext.Current.Server.HtmlEncode(item.Name, stringWriter);
                item.Name = stringWriter.ToString();
            }
        }
    }

    /// <summary>
    /// 局级考试列表的特殊字符转换
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static void ConvertEduExamHtmlCode(ref List<ExamsEducation> list)
    {
        if (list.Count > 0)
        {
            foreach (ExamsEducation item in list)
            {
                TextWriter stringWriter = new StringWriter();

                System.Web.HttpContext.Current.Server.HtmlEncode(item.Name, stringWriter);
                item.Name = stringWriter.ToString();
            }
        }
    }


    /// <summary>
    /// 学校考试列表的特殊字符转换
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static void ConvertSchoolExamHtmlCode(ref List<ExamsSchool> list)
    {
        if (list.Count > 0)
        {
            foreach (ExamsSchool item in list)
            {
                TextWriter stringWriter = new StringWriter();

                System.Web.HttpContext.Current.Server.HtmlEncode(item.Name, stringWriter);
                item.Name = stringWriter.ToString();
            }
        }
    }


    public static SchoolInfo SelectSohoolInfo()
    {
        if (!String.IsNullOrEmpty(GetSchoolMark))
        {
            ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", GetSchoolMark);
            List<SchoolInfo> list = (List<SchoolInfo>)schoolInfoLogic.FindAll(table);
            if (list.Count > 0)
                return list[0];
        }
        return null;
    }


    /// <summary>
    /// 局级过滤查询
    /// </summary>
    /// <param name="filter"></param>
    public static List<ExamsEducation> FilterTermGradeState(string filter, List<ExamsEducation> list, int term, int grade, int state)
    {
        List<ExamsEducation> newList = new List<ExamsEducation>();
        if (filter == "001")
        {
            foreach (ExamsEducation item in list)
            {
                if (item.State == state)
                    newList.Add(item);
            }
        }
        else if (filter == "010")
        {
            foreach (ExamsEducation item in list)
            {
                if (item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "011")
        {
            foreach (ExamsEducation item in list)
            {
                if (item.State == state && item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "100")
        {
            foreach (ExamsEducation item in list)
            {
                if (item.Term == term)
                    newList.Add(item);
            }
        }
        else if (filter == "101")
        {
            foreach (ExamsEducation item in list)
            {
                if (item.Term == term && item.State == state)
                    newList.Add(item);
            }
        }
        else if (filter == "110")
        {
            foreach (ExamsEducation item in list)
            {
                if (item.Term == term && item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "111")
        {
            foreach (ExamsEducation item in list)
            {
                if (item.Term == term && item.Grade == grade && item.State == state)
                    newList.Add(item);
            }
        }
        else
            newList = list;

        return newList;
    }

    /// <summary>
    /// 局级过滤查询
    /// </summary>
    /// <param name="filter"></param>
    public static List<VExamsEducation> FilterTermGradeState(string filter, List<VExamsEducation> targetList, int term, int grade, int state)
    {
        List<VExamsEducation> newList = new List<VExamsEducation>();
        List<VExamsEducation> list = new List<VExamsEducation>();
        if (state == 0)//未提交
        {
            foreach (VExamsEducation item in targetList)
            {
                if (item.Status == 0)
                    list.Add(item);
            }
        }
        else if (state == 1)//已提交
        {
            foreach (VExamsEducation item in targetList)
            {
                if (item.Status > 0)
                    list.Add(item);
            }
        }
        else
            list = targetList;

        if (filter == "001")
        {
            //foreach (VExamsEducation item in list)
            //{
            //    if (item.State == state)
            //        newList.Add(item);
            //}
            newList = list;
        }
        else if (filter == "010")
        {
            foreach (VExamsEducation item in list)
            {
                if (item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "011")
        {
            foreach (VExamsEducation item in list)
            {
                if (item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "100")
        {
            foreach (VExamsEducation item in list)
            {
                if (item.Term == term)
                    newList.Add(item);
            }
        }
        else if (filter == "101")
        {
            foreach (VExamsEducation item in list)
            {
                if (item.Term == term)
                    newList.Add(item);
            }
        }
        else if (filter == "110")
        {
            foreach (VExamsEducation item in list)
            {
                if (item.Term == term && item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "111")
        {
            foreach (VExamsEducation item in list)
            {
                if (item.Term == term && item.Grade == grade)
                    newList.Add(item);
            }
        }
        else
            newList = list;

        return newList;
    }

    /// <summary>
    /// 校级过滤查询
    /// </summary>
    /// <param name="filter"></param>
    public static List<ExamsSchool> FilterTermGradeState(string filter, List<ExamsSchool> list, int term, int grade)
    {
        List<ExamsSchool> newList = new List<ExamsSchool>();

        if (filter == "01")
        {
            foreach (ExamsSchool item in list)
            {
                if (item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "11")
        {
            foreach (ExamsSchool item in list)
            {
                if (item.Term == term && item.Grade == grade)
                    newList.Add(item);
            }
        }
        else if (filter == "10")
        {
            foreach (ExamsSchool item in list)
            {
                if (item.Term == term)
                    newList.Add(item);
            }
        }
        else if (filter == "11")
        {
            foreach (ExamsSchool item in list)
            {
                if (item.Term == term && item.Grade == grade)
                    newList.Add(item);
            }
        }
        else
            newList = list;

        return newList;
    }

    /// <summary>
    /// 获取学年下拉内容
    /// </summary>
    /// <returns></returns>
    public static string GetSchoolYearOption()
    {
        string s = "";
        s += " <option value='-1'>全部</option>"
            + "<option value='2020'>2020</option>"
            + "<option value='2019'>2019</option>"
            + "<option value='2018'>2018</option>" 
            + "<option value='2017'>2017</option>"
            + "<option value='2016'>2016</option>"
            + "<option value='2015'>2015</option>"
            + "<option value='2014'>2014</option>"
            + "<option value='2013'>2013</option>"
            + "<option value='2012'>2012</option>"
            + "<option value='2011'>2011</option>"
            + "<option value='2010'>2010</option>"
            + "<option value='2009'>2009</option>"
            + "<option value='2008'>2008</option>";
        return s;
    }

    /// <summary>
    /// 获取年级下拉内容
    /// </summary>
    /// <returns></returns>
    public static string GetGradeOption()
    {
        ISchoolInfoLogic logic = Container<ISchoolInfoLogic>.Instance;
        Hashtable table = new Hashtable();
        table.Add("SchoolMark", GetSchoolMark);
        List<SchoolInfo> list = (List<SchoolInfo>)logic.FindAll(table);
        string s = "";
        string junior = "<optgroup label='小学'>" +
                                        "<option value='1'>一年级</option>" +
                                        "<option value='2'>二年级</option>" +
                                        "<option value='3'>三年级</option>" +
                                        "<option value='4'>四年级</option>" +
                                        "<option value='5'>五年级</option>" +
                                        "<option value='6'>六年级</option>" +
                            "</optgroup>";

        string middle = " <optgroup label='中学'>" +
                                        "<option value='7'>初一</option>" +
                                        "<option value='8'>初二</option>" +
                                        "<option value='9'>初三</option>" +
                                    "</optgroup>";

        string high = " <optgroup label='高中'>" +
                                         "<option value='10'>高一</option>" +
                                         "<option value='11'>高二</option>" +
                                         "<option value='12'>高三</option>" +
                                    "</optgroup>  ";
        s = junior + middle + high;
        foreach (SchoolInfo item in list)
        {
            s = "";
            switch (item.SchoolOpt)
            {
                case 1://小学
                    s = junior;
                    break;
                case 2://初中 
                    s = middle;
                    break;
                case 3://高中 
                    s = high;
                    break;
                case 4://小+初 
                    s = junior + middle;
                    break;
                case 5://初+高 
                    s = middle + high;
                    break;
                default://小+初+高
                    s = junior + middle + high;
                    break;
            }
        }
        return s;
    }

    #region 获取图片样式
    //获取打开考试的图片路径
    public static string OpenExamSrc(int type,int currenttype, int current, int examId)
    {
        string s = "";
        if (type == 1)//局级
        {
            if (current == examId && type == currenttype)
                s = "../../Common/images/openExam1.gif";
            else
                s = "../../Common/images/chooseExam1.gif";
        }
        else
        {
            if (current == examId && type == currenttype)
                s = "../../Common/images/openExam1.gif";
            else
                s = "../../Common/images/chooseExam1.gif";
        }
        return s;
    }

    ////获取打开考试的背景色
    //public static string GetBackColor(int examId)
    //{
    //    string s = "";
    //    if (EducationCookie.ExamID == examId)
    //        s = "red";
    //    return s;
    //}
    //获取发布图片路径
    public static string PublishExamSrc(int state)
    {
        string s = "";
        if (state == 0)
            s = "../../Common/images/publishExam1.png";
        else
            s = "../../Common/images/publishExam2.png";
        return s;
    }

    //获取从考试录入的图片路径
    public static string InputByExamSrc(int status)
    {
        string s = "";
        if (status == 0)
            s = "../../Common/images/importByExam1.png";
        else
            s = "../../Common/images/importByExam2.png";
        return s;
    }

    //获取录入结果的图片路径
    public static string InputScoreSrc(int status)
    {
        string s = "";
        if (status == 0)
            s = "../../Common/images/import1.png";
        else
            s = "../../Common/images/import2.png";
        return s;

    }

    //获取上报考试的图片路径
    public static string ApplyExamSrc(int status)
    {
        string s = "";
        if (status == 0)
            s = "../../Common/images/applyExam1.png";
        else
            s = "../../Common/images/applyExam2.png";
        return s;

    }
    public static string GetOpenExamColor(int type, int current,int currenttype, int examId)
    {
        string s = "";
        if (type == 1)//局级
        {
            if(current == examId && type == currenttype)
                s =  "#fffccf";
            else
                s = "";
        }
        else
        {
            if (current == examId && type == currenttype)
                s =  "#fffccf";
            else
                s = "";
        }
        return s;
       
    }
    #endregion

    /// <summary>
    /// 获取学区ID（0表示所有学区）
    /// </summary>
    public static int GetAreaID
    {
        get 
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.SchoolAreaId; 
        }
    }

    /// <summary>
    /// 获取学区名
    /// </summary>
    public static string GetAreaName
    {
        get 
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.SchoolAreaName; 
        }
    }
    /// <summary>
    /// 获取登录学生信息
    /// </summary>
    public static StudentInfo LogonStudent
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            StudentInfo stu = new StudentInfo();
            stu.RID = p.UID;
            stu.StudentName = p.UName;
            stu.StudentCode = p.UserName; 
            return stu;
        }
    }

    /// <summary>
    /// 登录用户ID
    /// </summary>
    public static int GetUserID
    {
        get 
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.UID; 
        }
    }

    /// <summary>
    /// 登录用户Name
    /// </summary>
    public static string GetUserName
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.UName;
        }
    }
    /// <summary>
    /// 登录用户工号
    /// </summary>
    public static string GetUserCode
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.UserName;
        }
    }
    /// <summary>
    /// 获取学校标识(学校用户名)
    /// </summary>
    public static string GetSchoolMark
    {
        get 
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.SchoolMark; 
        }
    }

    /// <summary>
    /// 学校名字
    /// </summary>
    public static string SchoolName
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.SchoolName;
        }
    }
    /// <summary>
    /// 教育局 － 考试管理
    /// </summary>
    public static bool EduManageExam
    {
        get
        {

            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.Edu_Exam;
        }
    }

    /// <summary>
    /// 教育局 － 显示报表
    /// </summary>
    public static bool EduShowReport
    {
        get
        {

            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.Edu_Report;
        }
    }

    /// <summary>
    /// 学校 － 统考管理
    /// </summary>
    public static bool School_Edu_ManageExam
    {
        get
        {

            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.School_Score_EduExam;
        }
    }

    /// <summary>
    /// 学校 － 校考管理
    /// </summary>
    public static bool School_School_ShowReport
    {
        get
        {

            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.School_Score_SchoolExam;
        }
    }


    /// <summary>
    /// 学校 － 查看报表
    /// </summary>
    public static bool SchoolShowReport
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.School_Score_Report;
        }
    }
    /// <summary>
    /// 学分管理设置权限
    /// </summary>
    public static bool ManagerCredit
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.School_Credit_Role;
        }
    }
    /// <summary>
    /// 查看所有学分管理
    /// </summary>
    public static bool ShowCreditReport
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.School_Credit_Report;
        }
    }
    /// <summary>
    /// 德育管理设置权限
    /// </summary>
    public static bool ManagerMoral
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.School_Morality_Role;
        }
    }
    /// <summary>
    /// 查看所有德育
    /// </summary>
    public static bool ShowMoralReport
    {
        get
        {
            Perm p = System.Web.HttpContext.Current.Session["UserInfo"] == null ? new Perm() : (Perm)System.Web.HttpContext.Current.Session["UserInfo"];
            return p.School_Morality_Report;
        }
    }
    /// <summary>
    /// 获取模糊查询学生的sql字符串
    /// </summary>
    /// <param name="stuId">学生id</param>
    /// <param name="stuName">学生名</param>
    /// <returns></returns>
    public static string GetStuSelectInfo(string stuCode, string stuName)
    {
        string s = "and (";
        if (!string.IsNullOrEmpty(stuCode) && !string.IsNullOrEmpty(stuName))
            s += "studentinfo.studentname like '%" + stuName + "%' or studentinfo.studentcode like '%" + stuCode + "%' )";
        else if (!string.IsNullOrEmpty(stuCode))
            s += "studentinfo.studentcode like '%" + stuCode + "%')";
        else if (!string.IsNullOrEmpty(stuName))
            s += "studentinfo.studentname like '%" + stuName + "%' )";
        else
            s = "";
        return s;
    }

}
